package com._51doit.jdbc;

import com._51doit.beans.Student;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2021/8/24
 * @Created by HANGGE
 * @Description
 *
 */
public class Demo03 {
    public static void main(String[] args) throws Exception {
        // 1 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_doit26", "root", "root");
        // 3获取执行sql的对象
        Statement stm = conn.createStatement();
        // 4接收查询数据
        String sql = "select * from tb_stu";
        // 返回结果集  迭代器 迭代数据
        ResultSet resultSet = stm.executeQuery(sql);
        List<Student> list = new ArrayList<Student>();
        //是否有下一行数据
        while (resultSet.next()) {
           //获取所有字段数据  将每行数据封装哎一个类中  javaBean中
           // 1) 获取所有的字段
            String id = resultSet.getString(1);
            String name = resultSet.getString(2);
            int age = resultSet.getInt(3);
            String tel = resultSet.getString(4);
            String gender = resultSet.getString(5);
            double score = resultSet.getDouble(6);
            String address = resultSet.getString(7);
            //  2) 将字段封装在执行的属性中
            Student student = new Student(id,name,age,tel,gender,score,address);
            // 将每个JavaBean存储在List集合中
            list.add(student) ;
        }
        // 展示数据
        for (Student student : list) {
            System.out.println(student);
        }
        // 5释放资源
        resultSet.close();
        stm.close();
        conn.close();
    }
}
